package com.aquarium.sensor;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

//每个进度条都包含三段，第二段为下限值到上线值，第三段为上线值到缓冲上限，第一段为起点到下限值，可能为（上限-下限）的0~100%
public class SensorValueView extends View {
	private static final String TAG = "SensorValueView";
	// 上下限值以及当前值
	private float minValue = 0f;
	private float maxValue = 0f;
	private float currentValue = 0f;

	// 温度计中显示的端点
	private float lowerValue = 0f;
	private float upperValue = 0f;

	// view 的宽度
	private int width = 400;

	// 刻度的步进 (px)
	private int step = 0;
	// 刻度
	private int count = 0;

	// 代表上下限标记的距离 (px)，在所有图中都是一致的，即总长度的1/3
	private int length = 0;

	// 代表current value到起点的距离
	private int progressLength = 0;

	// 代表第一段的长度，起点到下限值的距离
	private int firstSegLength = 0;

	private Paint mLinePaint;
	private Paint mRedPaint;
	private Paint mGreenPaint;
	private Paint mGrayPaint;
	private Paint mTextPaint;

	public SensorValueView(Context context) {
		super(context);
		mLinePaint = new Paint();
		mRedPaint = new Paint();
		mRedPaint.setColor(Color.RED);
		mGreenPaint = new Paint();
		mGreenPaint.setColor(Color.GREEN);
		mGrayPaint = new Paint();
		mGrayPaint.setColor(Color.GRAY);
		mTextPaint = new Paint();
		mTextPaint.setTextSize(10);
	}

	public SensorValueView(Context context, AttributeSet attr) {
		super(context, attr);
		mLinePaint = new Paint();
		mRedPaint = new Paint();
		mRedPaint.setColor(Color.RED);
		mGreenPaint = new Paint();
		mGreenPaint.setColor(Color.GREEN);
		mGrayPaint = new Paint();
		mGrayPaint.setColor(Color.GRAY);
		mTextPaint = new Paint();
		mTextPaint.setTextSize(10);

		length = width / 3;
	}

	public void setCurrentValue(float currentValue, float minValue,
			float maxValue) {

		this.currentValue = currentValue;
		this.minValue = minValue;
		this.maxValue = maxValue;

		this.lowerValue = minValue - (maxValue - minValue);
		if (this.lowerValue < 0) {
			firstSegLength = (int) ((1 - Math.abs(lowerValue)
					/ (maxValue - minValue)) * length);
			this.lowerValue = 0;
		} else {
			firstSegLength = length;
		}
		this.upperValue = maxValue + (maxValue - minValue);
		invalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {

		Log.e(TAG, "current value:" + currentValue + " minValue" + minValue
				+ " maxValue" + maxValue + " firstLength:" + firstSegLength);

		super.onDraw(canvas);

		if (currentValue < lowerValue) {
			progressLength = 0;
			canvas.drawRect(0, 40, progressLength, 50, mRedPaint);

		} else if (currentValue < minValue) {
			progressLength = (int) ((currentValue - lowerValue)
					/ (minValue - lowerValue) * firstSegLength);
			canvas.drawRect(0, 40, progressLength, 50, mRedPaint);

		} else if (currentValue < maxValue) {
			progressLength = (int) ((currentValue - minValue)
					/ (maxValue - minValue) * length)
					+ firstSegLength;
			canvas.drawRect(0, 40, progressLength, 50, mGreenPaint);

		} else if (currentValue < upperValue) {
			progressLength = (int) ((currentValue - maxValue)
					/ (upperValue - currentValue) * length)
					+ firstSegLength + length;
			canvas.drawRect(0, 40, progressLength, 50, mRedPaint);
		} else {
			progressLength = firstSegLength + 2 * length;
			canvas.drawRect(0, 40, progressLength, 50, mRedPaint);
		}

		// 绘制 current value标志线
		canvas.drawLine(progressLength, 20, progressLength, 40, mLinePaint);
		// 绘制 min value标志线
		canvas.drawLine(firstSegLength, 30, firstSegLength, 40, mLinePaint);
		// 绘制 max value标志线
		canvas.drawLine(firstSegLength + length, 30, firstSegLength + length,
				40, mLinePaint);

		// 绘制起点到current value的矩形区域，需要根据位置来采用不同paint
		// 绘制current value到终点的矩形区域，一律用灰色paint
		canvas.drawRect(progressLength, 40, firstSegLength + 2 * length, 50,
				mGrayPaint);

		// 绘制current value和min value , max value 文字
		canvas.drawText(currentValue + "", progressLength, 10, mTextPaint);
		canvas.drawText(minValue + "", firstSegLength, 30, mTextPaint);
		canvas.drawText(maxValue + "", firstSegLength + length, 30, mTextPaint);

	}
}
